package com.player.thirdGroup.service;
import com.player.thirdGroup.dao.DeptDao;
import com.player.thirdGroup.dao.LogDao;
import com.player.thirdGroup.dao.MessageDao;
import com.player.thirdGroup.dao.PlayerDao;
import com.player.thirdGroup.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author VULCAN
 * @create 2019-12-28 7:48
 * @desc 单位
 **/
@Service
public class DeptService {
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private LogDao logDao;
    @Autowired
    private MessageDao messageDao;
    @Autowired
    private PlayerDao playerDao;
    public List<Dept> findAll() {
        return deptDao.findAll();
    }
    //通过
    public Dept deptunderpass(String id) throws UnknownHostException {
        Dept dept = deptDao.findById(id).get();
        //通过
        dept.setStatus("1");
        dept.setCdate(new Date());
        deptDao.save(dept);
        //通过日志
        Log log = new Log();
        log.setId(UUID.randomUUID().toString());
        log.setDate(new Date());
        log.setName(dept.getPerson());
        InetAddress ip4 = Inet4Address.getLocalHost();
        System.out.println(ip4.getHostAddress());
        log.setIp(ip4.getHostAddress());
        log.setContent("单位审核-" + dept.getName() + "-审核成功");
        logDao.save(log);
        return dept;
    }
    //不通过
    public Dept deptundernopass(String id) throws UnknownHostException {
        Dept dept = deptDao.findById(id).get();
        //不通过
        dept.setStatus("2");
        dept.setCdate(new Date());
        deptDao.save(dept);
        //不通过日志
        Log log = new Log();
        log.setId(UUID.randomUUID().toString());
        log.setDate(new Date());
        log.setName(dept.getPerson());
        InetAddress ip4 = Inet4Address.getLocalHost();
        System.out.println(ip4.getHostAddress());
        log.setIp(ip4.getHostAddress());
        log.setContent("单位审核-" + dept.getName() + "-审核失败");
        logDao.save(log);

        return dept;
    }

    /**
     * 批量通过
     */
    public String bulkpass(Long[] ids) throws UnknownHostException {
        for (Long id : ids) {
            Dept dept = deptDao.findById(id + "").get();
            dept.setStatus("1");
            dept.setCdate(new Date());
            deptDao.save(dept);
            //通过日志
            Log log = new Log();
            log.setId(UUID.randomUUID().toString());
            log.setDate(new Date());
            log.setName(dept.getPerson());
            InetAddress ip4 = Inet4Address.getLocalHost();
            System.out.println(ip4.getHostAddress());
            log.setIp(ip4.getHostAddress());
            log.setContent("单位审核-" + dept.getName() + "-审核成功");
            logDao.save(log);
        }
        return null;
    }

    /**
     * 批量不通过
     */
    public String bulknopass(Long[] ids) throws UnknownHostException {
        for (Long id : ids) {
            Dept dept = deptDao.findById(id + "").get();
            dept.setStatus("2");
            dept.setCdate(new Date());
            deptDao.save(dept);
            //不通过日志
            Log log = new Log();
            log.setId(UUID.randomUUID().toString());
            log.setDate(new Date());
            log.setName(dept.getPerson());
            InetAddress ip4 = Inet4Address.getLocalHost();
            System.out.println(ip4.getHostAddress());
            log.setIp(ip4.getHostAddress());
            log.setContent("单位审核-" + dept.getName() + "-审核失败");
            logDao.save(log);
        }
        return null;
    }
    /**
     * 计算审核通过数量
     * @return
     */
    public String countpass() {
        return deptDao.countpass();
    }
    /**
     * 计算审核不通过数量
     * @return
     */
    public String countnopass() {
        return deptDao.countnopass();
    }
    /**
     * 计算审核中数量
     * @return
     */
    public String countunder() {
        return deptDao.countunder();
    }
    /**
     * 更改状态
     * @return
     */
    public List<Dept> updatestatus(String status) {
        return deptDao.findByStatus(status);
    }

    public void deptAdd(Dept dept) {
        deptDao.save(dept);
    }
    /**
     * 驳回
     * @return
     */
    public void deptreason(String id, User user) {
        Dept dept = deptDao.findById(id).get();
        System.out.println(dept);
        Message message = new Message();
        message.setId(UUID.randomUUID().toString());
        message.setDate(new Date());
        message.setPark(dept.getPerson() + " 审核未通过 ：驳回原因是：" + user.getUsername());
        message.setKey(id);
        messageDao.save(message);
    }
    /**
     * 批量驳回
     * @return
     */
    public void deptreasonids(Long[] ids, User user) {
        for (Long id : ids) {
            Dept dept = deptDao.findById(id + "").get();
            Message message = new Message();
            message.setId(UUID.randomUUID().toString());
            message.setDate(new Date());
            message.setPark(dept.getPerson() + " 审核未通过 ：驳回原因是：" + user.getUsername());
            messageDao.save(message);
        }

    }
    /**
     * 查看驳回
     * @return
     */
    public Message lookturn(String id) {
        Message byKey = messageDao.findByKey(id);
        return byKey;
    }
    /**
     * 单位管理列表
     * @return
     */
    public List<Dept> administrationlist () {
            return deptDao.findAll();
    }
    public void accountDelete(String id) {
        deptDao.deleteById(id);
    }

    public List<Player> lookplayer(String id) {
        List<Player> bydeptid = playerDao.findBydeptid(id);
        System.out.println(bydeptid);
        return bydeptid;
    }
}
